package com.shigu.address.service;

import com.opentae.data.mall.beans.MallArea;
import com.opentae.data.mall.beans.MallCity;
import com.opentae.data.mall.beans.MallProv;
import com.opentae.data.mall.beans.MallUserAddr;
import com.opentae.data.mall.examples.MallUserAddrExample;
import com.opentae.data.mall.interfaces.MallAreaMapper;
import com.opentae.data.mall.interfaces.MallCityMapper;
import com.opentae.data.mall.interfaces.MallProvMapper;
import com.opentae.data.mall.interfaces.MallUserAddrMapper;
import com.shigu.address.bo.ModUserAddressBo;
import com.shigu.address.bo.UserAddressBo;
import com.shigu.exceptions.WapException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Mr.赵
 * created on 2018/10/24
 */
@Service
public class AddressAdminService {

    @Autowired
    MallUserAddrMapper mallUserAddrMapper;
    @Autowired
    MallProvMapper mallProvMapper;
    @Autowired
    MallCityMapper mallCityMapper;
    @Autowired
    MallAreaMapper mallAreaMapper;

    /**
     * 添加收货地址
     * @param userId 用户ID
     * @return
     */
    public Long addAddress(Long userId, UserAddressBo bo) throws WapException {
        MallUserAddrExample example=new MallUserAddrExample();
        example.createCriteria().andUserIdEqualTo(userId);
        if (mallUserAddrMapper.countByExample(example)==0) {
             bo.setImDefault(true);
        }
        MallProv prov = mallProvMapper.selectByPrimaryKey(bo.getProvId());
        MallCity city = mallCityMapper.selectByPrimaryKey(bo.getCityId());
        MallArea area = mallAreaMapper.selectByPrimaryKey(bo.getAreaId());
        MallUserAddr addr=new MallUserAddr();

        addr.setAreaId(bo.getAreaId());
        addr.setAreaName(area.getName());
        addr.setCityId(bo.getCityId());
        addr.setCityName(city.getName());
        addr.setProvId(bo.getProvId());
        addr.setProvName(prov.getName());

        addr.setImDefault(bo.isImDefault());
        addr.setUserId(userId);
        addr.setReceiveName(bo.getReceiveName());
        addr.setReceiveTelephone(bo.getReceiveTelephone());
        addr.setAddressDetail(bo.getReceiveAddress());
        mallUserAddrMapper.insertSelective(addr);
        if (bo.isImDefault()) {
            setDefaultAddress(userId,addr.getAddrId());
        }
        return addr.getAddrId();
    }

    /**
     * 把地址设置为默认地址
     * @param userId 用户ID
     * @param addressId 地址ID
     */
    public void setDefaultAddress(Long userId,Long addressId) throws WapException {
        //先验证addressId是不是这个用户的
        isMyAddress(userId,addressId);
        MallUserAddrExample example=new MallUserAddrExample();
        example.createCriteria().andUserIdEqualTo(userId);
        MallUserAddr upaddr=new MallUserAddr();
        upaddr.setImDefault(false);
        mallUserAddrMapper.updateByExampleSelective(upaddr,example);
        upaddr.setImDefault(true);
        upaddr.setAddrId(addressId);
        mallUserAddrMapper.updateByPrimaryKeySelective(upaddr);
    }

    /**
     * 验证地址是本用户下的
     * @param userId
     * @param addressId
     */
    private void isMyAddress(Long userId,Long addressId) throws WapException {
        MallUserAddr addr=mallUserAddrMapper.selectByPrimaryKey(addressId);
        if (addr == null) {
            throw new WapException("地址不存在");
        }
        if (!addr.getUserId().equals(userId)) {
            throw new WapException("只能修改自己的地址");
        }
    }
    /**
     * 删除地址
     * @param addressId 地址ID
     */
    public void delAddress(Long userId,Long addressId) throws WapException {
        isMyAddress(userId,addressId);
        //如果当关地址是默认的，删除完后查出一个新的设置为默认
        MallUserAddr addr=mallUserAddrMapper.selectByPrimaryKey(addressId);
        mallUserAddrMapper.deleteByPrimaryKey(addressId);
        if (addr != null&&addr.getImDefault()) {
            MallUserAddrExample example=new MallUserAddrExample();
            example.createCriteria().andUserIdEqualTo(userId);
            example.setStartIndex(0);
            example.setEndIndex(1);
            example.setOrderByClause("addr_id desc");
            List<MallUserAddr> mylastAddr=mallUserAddrMapper.selectByConditionList(example);
            if (mylastAddr.size()>0) {
                setDefaultAddress(userId,mylastAddr.get(0).getAddrId());
            }
        }
    }

    /**
     * 更新收货地址
     * @param userId 用户ID
     *
     */
    public void updateAddress(Long userId, ModUserAddressBo bo) throws WapException {
        isMyAddress(userId,bo.getAddressId());

        MallProv prov = mallProvMapper.selectByPrimaryKey(bo.getProvId());
        MallCity city = mallCityMapper.selectByPrimaryKey(bo.getCityId());
        MallArea area = mallAreaMapper.selectByPrimaryKey(bo.getAreaId());
        MallUserAddr addr=new MallUserAddr();

        addr.setAddrId(bo.getAddressId());

        addr.setAreaId(bo.getAreaId());
        addr.setAreaName(area.getName());
        addr.setCityId(bo.getCityId());
        addr.setCityName(city.getName());
        addr.setProvId(bo.getProvId());
        addr.setProvName(prov.getName());

        addr.setImDefault(bo.isImDefault());
        addr.setUserId(userId);
        addr.setReceiveName(bo.getReceiveName());
        addr.setReceiveTelephone(bo.getReceiveTelephone());
        addr.setAddressDetail(bo.getReceiveAddress());
        mallUserAddrMapper.updateByPrimaryKeySelective(addr);

        if (bo.isImDefault()) {
            setDefaultAddress(userId,bo.getAddressId());
        }
    }


}
